package com.caishi.lkx.exam.model;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zzw.common.entity.impl.BaseEntity;
import com.zzw.common.service.model.NameModel;
import com.zzw.common.service.model.base.SortModel;
import com.caishi.lkx.exam.QuestionCollection;
import com.caishi.lkx.exam.QuestionCollectionBlockItem;
import com.caishi.lkx.exam.ienum.type.PaperType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author by Jansia
 * @Date 2022/1/20 15:26
 */
@Schema(title = "试卷")
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "ex_paper", autoResultMap = true)
public class PaperModel extends BaseEntity<PaperModel> implements NameModel<PaperModel, String>, QuestionCollection<PaperModel, String>, SortModel<PaperModel>, CstkModel {

    @Schema(title = "学科id")
    private Long categoryId;

    @Schema(title = "试卷名称")
    private String name;

    /**
     * 后台显示名称
     */
    private String serverName;

    @Schema(title = "试卷备注")
    private String remark;

    /**
     * 0完整 1暂缺答案 2暂缺音频 3试题不全
     */
    @Schema(title = "完整度")
    private Integer integrityState;

    @Schema(title = "所属年份")
    private String sourceYear;

    @Schema(title = "考试时长")
    private Integer examTime;

    @Schema(title = "总分")
    private Integer totalScore;

    @Schema(title = "及格分")
    private Integer passScore;

    @Schema(title = "所属区域")
    private String region;

    @Schema(title = "来源id")
    private String sourceId;

    /**
     * 0加工，1vers6.0导入，2才士导入 3 散题
     */
    @Schema(title = "资源类型")
    private Integer sourceType;

    @Schema(title = "试卷类型")
    private PaperType paperType;

    @Schema(title = "学习人数")
    private Integer stuCount;

    @Schema(title = "是否隐藏试卷")
    private Boolean isHide;

    @Schema(title = "排序")
    private Integer sort;

    @Schema(title = "试题数量")
    private Integer qcount;

    @Schema(title = "查看次数")
    private Integer watchTime;

    @Schema(title = "高教题库id")
    private Integer highEduId;

    @Schema(title = "考试id")
    @TableField(exist = false)
    private String examId;

    @Schema(title = "考试名")
    @TableField(exist = false)
    private String examName;

    @Schema(title = "行业id")
    @TableField(exist = false)
    private String industryId;

    @Schema(title = "行业名")
    @TableField(exist = false)
    private String industryName;

    @Schema(title = "考研科目id")
    private Long kaoyanCategoryId;

    @Schema(title = "旧科目id")
    private Long oldCategoryId;

    @Schema(title = "试题ids")
    @TableField(value = "question_ids", typeHandler = JacksonTypeHandler.class)
    private List<String> questionIds;

    @Override
    public String getCstkId() {
        if (sourceType != null && sourceType == 2) {
            return sourceId;
        }
        return null;
    }

    @Override
    public void setCstkId(String id) {
        sourceId = id;
        sourceType = 2;
    }

    @TableField(value = "content", typeHandler = JacksonTypeHandler.class)
    @Schema(title = "试卷内容")
    @JsonIgnore
    private List<Map<String, Object>> contentMap;

    @TableField(exist = false)
    @Schema(title = "学科名")
    private String categoryName;

    @TableField(exist = false)
    @Schema(title = "学科名(旧)")
    private String oldCategoryName;

    @TableField(exist = false)
    @Schema(title = "栏目id")
    private String columnId;

    @TableField(exist = false)
    private Boolean free;


    public List<QuestionCollectionBlockItem> getContent() {
        if (contentMap != null) {
            return contentMap.stream().map(v -> BeanUtil.fillBeanWithMap(v, new QuestionCollectionBlockItem(), true)).toList();
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public void setContent(List<QuestionCollectionBlockItem> content) {
        if (content == null) {
            this.contentMap = null;
            return;
        }
        this.contentMap = content.stream().map(v -> JSON.parseObject(JSON.toJSONString(v)).getInnerMap()).toList();
    }
}
